package torres.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JTextArea;

import torres.map.Location;
import torres.map.Map;
import torres.map.World;
import torres.entity.Avatar;

/**
 * @author Torres
 * In charge of actually drawing the ascii character in the screen
 * as a graphic model
 */
@SuppressWarnings("serial")
public class AsciiAreaViewPort extends AreaViewPort {
	private static final int MAP_STRING_SIZE = 1000;
	private JTextArea asciiArea;
	private Map map = world.getCurrentMap();
	private Font font;
	private StringBuilder mapString = new StringBuilder(MAP_STRING_SIZE);
	
	/**
	 * Creates a new AsciiAreaViewPort with the default settings.
	 */
	public AsciiAreaViewPort() {	
		this.setBackground(Color.BLACK);
		asciiArea = new JTextArea();
		asciiArea.setEditable(false);
		asciiArea.setFont(initAndGetFont() );
		asciiArea.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT));
		
		asciiArea.setBackground(Color.BLACK);
		asciiArea.setForeground(Color.WHITE);
		this.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT));
		this.add(asciiArea, BorderLayout.WEST);
		setVisible(true);
		//Display the window
	}
	
	/**
	 * Prints the specified String to the ASCII viewport
	 * @param s The String to be printed
	 */
	public void asciiPrint(String s) {
		
		asciiArea.setText(s);
	}
	
	/**
	 * Returns a JTextArea representing the ASCII drawing surface
	 * @return The ASCII drawing surface.
	 */
	public JTextArea getAsciiArea() {
		return this.asciiArea;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#setFont(java.awt.Font)
	 */
	public void setFont(Font f) {	
	
		this.font = f;
	}
	
	/* (non-Javadoc)
	 * @see java.awt.Component#getFont()
	 */
	public Font getFont() {
		
		return font;
	}
	
	/**
	 * Returns the default Font
	 * @return The default Font
	 */
	private Font initAndGetFont() {
		
		return new Font("Courier", Font.PLAIN, 40);
	}
	
	/**
	 * Clears the text of the ASCII map.
	 */
	public void clearMap() {
		
		asciiArea.setText("");
	}
	
	/* (non-Javadoc)
	 * @see torres.view.AreaViewPort#setCurrentMap(torres.map.Map)
	 */
	public void setCurrentMap(Map map) {
		this.map = map;
	}
	
	/* (non-Javadoc)
	 * @see torres.view.AreaViewPort#draw()
	 */
	public void draw() {		
		
		map = World.getInstance().getCurrentMap();
		Avatar avatar = map.getAvatar();
		Location location = avatar.getLocation();
		mapString.replace(0, mapString.length(), "");
		for(int i = location.getY() - 2; i < location.getY() + 3; i++)
		{
			for(int j = location.getX() - 2; j < location.getX() + 3; j++)
			{				
				mapString.append("+---");
			}
			mapString.append("+\n");
	
			for(int j = location.getX() - 2; j < location.getX() + 3; j++)
			{				
				if(map.getTiles()[i][j].getEntity() != null) {
					
					mapString.append("| ").append(map.getTiles()[i][j].getEntity().toString()).append(" ");
				}
				else if(map.getTiles()[i][j].getItem() != null) {
					
					mapString.append("| ").append(map.getTiles()[i][j].getItem().getAsciiRep()).append(" ");
				}
				else {
					
					/*if (map.getTiles()[i][j].getTerrain().toString().equalsIgnoreCase(" G ")) {
						mapString.append("| " + " " + " ");
					}*/
					//else {
						mapString.append("|").append(map.getTiles()[i][j].toString());
					//}
				}
			}
			mapString.append("|\n");
		}
		for(int j = location.getX() - 2; j < location.getX() + 3; j++)
		{				
			mapString.append("+---");
		}
		mapString.append("+\n");
		asciiPrint(mapString.toString());
	}
	
	/* (non-Javadoc)
	 * @see torres.view.AreaViewPort#getArea()
	 */
	public JTextArea getArea() {
		return asciiArea;
	}
	
	/* (non-Javadoc)
	 * @see torres.view.AreaViewPort#isAscii()
	 */
	@Override
	public boolean isAscii() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see torres.view.AreaViewPort#isTwoD()
	 */
	@Override
	public boolean isTwoD() {
		return false;
	}
}